package com.atguigu.gulimall.order.controller;

import com.atguigu.gulimall.order.service.OrderService;
import com.atguigu.gulimall.order.vo.OrderConfirmVo;
import com.atguigu.gulimall.order.vo.SubmitOrderResponseVo;
import com.atguigu.gulimall.order.vo.SubmitOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/toTrade")
    public String toTrade(Model model){

        OrderConfirmVo confirmVo = orderService.confirmOrder();
        model.addAttribute("orderConfirmData",confirmVo);
        return "confirm";
    }

    @PostMapping("/submitOrder")
    public String sunmitOrder(@RequestBody SubmitOrderVo vo,Model model){

        SubmitOrderResponseVo submitOrderVo = orderService.submitOrder(vo);
        if (submitOrderVo.getCode() == 0){
            model.addAttribute("submitOrderVo",submitOrderVo);
            return "pay";
        }else {
            //失败,就重定向到下单列表
            return "redirect:http//order.gulimall.com/toTrade";
        }
    }
}
